package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.SysBuildingUnitMapper;
import com.ruoyi.system.mapper.SysTenantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysBuildingMapper;
import com.ruoyi.system.service.ISysBuildingService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 楼栋信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-06-23
 */
@Service
public class SysBuildingServiceImpl implements ISysBuildingService 
{
    @Autowired
    private SysBuildingMapper sysBuildingMapper;

    @Autowired
    private SysBuildingUnitMapper sysBuildingUnitMapper;

    @Autowired
    private SysTenantMapper sysTenantMapper;

    /**
     * 查询楼栋信息
     * 
     * @param buildingId 楼栋信息ID
     * @return 楼栋信息
     */
    @Override
    public SysBuilding selectSysBuildingById(Long buildingId)
    {
        return sysBuildingMapper.selectSysBuildingById(buildingId);
    }

    /**
     * 根据单元查询楼栋信息
     *
     * @param untiId 单元ID
     * @return 楼栋信息
     */
    @Override
    public List<SysBuilding> selectSysBuildingByUnitId(Long untiId) {
        SysBuilding building =  sysBuildingMapper.selectSysBuildingByUnitId(untiId);
        List<SysBuilding> buildings = sysBuildingMapper.selectSysBuildingAll();
        for (SysBuilding sysBuilding : buildings) {
            if (sysBuilding.getBuildingId().longValue() == building.getBuildingId().longValue()) {
                sysBuilding.setFlag(true);
                break;
            }
        }
        return buildings;
    }

    /**
     * 单元信息展示
     *
     * @param sysBuilding
     * @return 楼栋信息
     */
    @Override
    public List<SysBuildingVo> unitShowlist(SysBuilding sysBuilding) {
        return sysBuildingMapper.unitShowlist(sysBuilding);
    }

    /**
     * 根据租户查询楼栋信息
     *
     * @param tenantId 租户ID
     * @return 楼栋信息
     */
    @Override
    public List<SysBuilding> selectSysBuildingByTenantId(Long tenantId) {
        SysTenant sysTenant = sysTenantMapper.selectSysTenantById(tenantId);
        List<SysBuilding> buildings = sysBuildingMapper.selectSysBuildingAll();
        for (SysBuilding sysBuilding : buildings) {
            if (sysBuilding.getBuildingId().longValue() == sysTenant.getBuildingId().longValue()) {
                sysBuilding.setFlag(true);
                break;
            }
        }
        return buildings;
    }

    /**
     * 查询楼栋信息列表
     * 
     * @param sysBuilding 楼栋信息
     * @return 楼栋信息
     */
    @Override
    public List<SysBuilding> selectSysBuildingList(SysBuilding sysBuilding)
    {
        return sysBuildingMapper.selectSysBuildingList(sysBuilding);
    }

    /**
     * 查询所有楼栋信息
     *
     * @return 楼栋信息
     */
    @Override
    public List<SysBuilding> selectSysBuildingAll()
    {
        return sysBuildingMapper.selectSysBuildingAll();
    }

    /**
     * 新增楼栋信息
     * 
     * @param sysBuilding 楼栋信息
     * @return 结果
     */
    @Override
    public int insertSysBuilding(SysBuilding sysBuilding)
    {
        SysBuilding findSysBuilding = new SysBuilding();
        findSysBuilding.setBuildingName(sysBuilding.getBuildingName());
        List<SysBuilding> list = sysBuildingMapper.selectSysBuildingList(findSysBuilding);
        if(list!=null && !list.isEmpty()){
            return 0;
        }
        sysBuilding.setCreateTime(DateUtils.getNowDate());
        return sysBuildingMapper.insertSysBuilding(sysBuilding);
    }

    /**
     * 修改楼栋信息
     * 
     * @param sysBuilding 楼栋信息
     * @return 结果
     */
    @Override
    public int updateSysBuilding(SysBuilding sysBuilding)
    {
        sysBuilding.setUpdateTime(DateUtils.getNowDate());
        return sysBuildingMapper.updateSysBuilding(sysBuilding);
    }

    /**
     * 删除楼栋信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysBuildingByIds(String ids)
    {
        List<SysBuildingUnit> list = sysBuildingUnitMapper.selectSysBuildingUnitByBuildingIds(Convert.toStrArray(ids));
        if(list!=null && !list.isEmpty()){
            return 0;
        }
        return sysBuildingMapper.deleteSysBuildingByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除楼栋信息信息
     * 
     * @param buildingId 楼栋信息ID
     * @return 结果
     */
    @Override
    public int deleteSysBuildingById(Long buildingId)
    {
        SysBuildingUnit sysBuildingUnit = sysBuildingUnitMapper.selectSysBuildingUnitByBuildingId(buildingId);
        if(sysBuildingUnit!=null){
            return 0;
        }
        return sysBuildingMapper.deleteSysBuildingById(buildingId);
    }

}
